#!/bin/bash
#
# description: $PWNAME server
#
# Get function from functions library
#
. /lib/lsb/init-functions

### BEGIN INIT INFO
# Provides: propcap
# Required-Start: 
# Required-Stop: 
# Should-Start: 
# Should-Stop: 
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and Stop 
# Description: Propeller Capp (propcap) Web IDE
### END INIT INFO

PWNAME=propcap
PWDIR=/opt/$PWNAME
PWEBAPP="/usr/local/bin/node $PWDIR/server.js"
PWALOCK=/var/lock/$PWNAME
PWALOG=/var/log/$PWNAME
PROPGCC=/opt/parallax/bin

# Start the service
start() {
  if [ -e $PWALOCK ]
  then
    echo "$PWNAME server already running"
  else
    export PATH=$PROPGCC:$PATH
    cd $PWDIR
    #$PWEBAPP > $PWALOG &
    $PWEBAPP &

    ### Create the lock file ###
    echo $! > $PWALOCK
    PID=`cat $PWALOCK`
    echo -n "started $PWNAME server on PID: "
    echo $PID
  fi
}

# Stop the service $PWNAME
stop() {
  if [ -e $PWALOCK ]
  then
    echo -n "stopping $PWNAME server on PID: "
    PID=`cat $PWALOCK`
    echo $PID
    kill -9 $PID

    ### Now, delete the lock file ###
    rm -f $PWALOCK
  echo
  fi
}

### main logic ###
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $PWNAME
    ;;
  restart|reload|condrestart)
    stop
    start
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|reload|status}"
    exit 1
esac
exit 0

